/**
 * 
 */
package com.blog.controller;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.MultipartResolver;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import com.blog.domain.Album;
import com.blog.domain.Photo;
import com.blog.domain.User;
import com.blog.utility.BlogConstants;

/**
 * @author cloris
 * 
 */
@Controller
@RequestMapping(value = "/album.do")
public class AlbumController extends BaseAction{

	@RequestMapping(params = "albums")
	public String getAlbumInfo(ServletRequest request, ServletResponse response,ModelMap modelMap) {
		List<Album> albums = blogService.queryAlbumDetails();
		modelMap.put("albums", albums);
		return "/views/album/albumMenuShow.jsp";
	}

	@RequestMapping(params = "cate")
	public String getAlbumCategory(ServletRequest request,
			ServletResponse response,ModelMap modelMap) {
		List<Album> albums = blogService.queryAlbums();
		modelMap.put("albums", albums);
		return "/views/album/uploadPhoto.jsp";					
	}

	@RequestMapping(params = "upload_photo")
	public String upimg(int type,ServletRequest request, ServletResponse response,ModelMap modelMap)throws IOException {
		HttpServletRequest hrequest = (HttpServletRequest) request;
		MultipartResolver resolver = new CommonsMultipartResolver(hrequest.getSession().getServletContext());
		MultipartHttpServletRequest multipartRequest = resolver.resolveMultipart(hrequest);
		MultipartFile file = multipartRequest.getFile("photoUrl");
		String message = "图片上传成功";
		if (file.isEmpty()) {
			message = "请选择上传照片";
		} else {
			String filename = file.getOriginalFilename();
			String imgtype = filename.substring(filename.lastIndexOf("."));
			if (!imgtype.equalsIgnoreCase(".jpg")&& !imgtype.equalsIgnoreCase(".jpeg")&& !imgtype.equalsIgnoreCase(".gif")) {
				message = "只能上传 jpg,jpeg,gif格式的 照片";
			} else if (file.getSize() > BlogConstants.ImgMaxSize) {
				message = "图片大小不能超过5M";
			} else {
				String ctxPath = hrequest.getSession().getServletContext().getRealPath("/")+BlogConstants.imgPath;
				File dirPath = new File(ctxPath);
				if (!dirPath.exists()) {
					dirPath.mkdirs();
				}
				File uploadFile = new File(ctxPath + Math.random()
						+ imgtype);
				FileCopyUtils.copy(file.getBytes(), uploadFile);
				hrequest.setAttribute("picpath", BlogConstants.imgPath+uploadFile.getName());			
			}
		}

			modelMap.put("msg", message);
			String path = "/views/album/uploadPhoto.jsp";
			if(type==1) path ="/views/home/changePhoto.jsp";
			return path;
			
	}
	@RequestMapping(params = "goto_change_photo")
	public String gotoChangePhoto(ServletRequest request, ServletResponse response,ModelMap modelMap)throws IOException {
		HttpServletRequest hrequest = (HttpServletRequest) request;
		User usr = (User) hrequest.getSession().getAttribute("user");
		modelMap.put("picpath",usr.getPhoto());
		return "/views/home/changePhoto.jsp";
	}
	
	@RequestMapping(params = "save_photo")
	public String savePhoto(String title,String comment,String albumId,String path,ServletRequest request, ServletResponse response,ModelMap modelMap)throws IOException {
		if (title != null && comment != null) {
			try {
				title = new String(title.getBytes("ISO8859_1"), "UTF8");
				comment = new String(comment.getBytes("ISO8859_1"),
						"UTF8");
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}				
		}		
		Photo photo = new Photo();
		photo.setAlbumId(Integer.parseInt(albumId));
		photo.setComment(comment);
		photo.setName(title);
		photo.setPath(path);
		blogService.savePhoto(photo);	
		modelMap.put("picpath",path);		
		modelMap.put("msg", "图片保存成功");
		return "/views/album/uploadPhoto.jsp";
	}
	
	@RequestMapping(params = "change_photo")
	public String changePhoto(ServletRequest request, ServletResponse response)throws IOException {
		HttpServletRequest hrequest = (HttpServletRequest) request;
		String path = request.getParameter("ppath");
		User usr = (User) hrequest.getSession().getAttribute("user");
		blogService.updateUserPhoto(usr.getUserId(), path);
		usr.setPhoto(path);
		return "/views/home/homeMenuShow.jsp";
	}
	
	@RequestMapping(params = "add_album")
	public String addAlbum(String name,String com,ServletRequest request, ServletResponse response,ModelMap modelMap)throws IOException {
		if (name == null || com == null) {
			modelMap.put("msg", "新建相册失败");
//			return 
//			try {
//				name = new String(name.getBytes("ISO8859_1"), "UTF8");
//				com = new String(com.getBytes("ISO8859_1"), "UTF8");
//			} catch (UnsupportedEncodingException e) {
//				e.printStackTrace();
//			}				
		}else{
			Album album = new Album();
			album.setName(name);
			album.setComment(com);
			album.setShow(1);
			album.setPicNum(0);
			blogService.saveAlbum(album);
			modelMap.put("msg", "成功新建相册");
		}		

		
		return "/views/album/addAlbum.jsp";
	}
	
}
